#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/string.hpp>
#include <QApplication>
#include <QPushButton>
#include <QWidget>
#include <QObject>

class QtRos2Node : public QWidget
{
    Q_OBJECT  // 必须有这个宏，确保元对象系统正常工作

public:
    explicit QtRos2Node(rclcpp::Node::SharedPtr node, QWidget *parent = nullptr);
    ~QtRos2Node() override;

private slots:
    void send_message();

private:
    rclcpp::Node::SharedPtr node_;
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
};

QtRos2Node::QtRos2Node(rclcpp::Node::SharedPtr node, QWidget *parent)
    : QWidget(parent), node_(node)
{
    QPushButton *button = new QPushButton("Send ROS 2 Message", this);
    button->setGeometry(50, 50, 200, 50);
    connect(button, &QPushButton::clicked, this, &QtRos2Node::send_message);
}

QtRos2Node::~QtRos2Node()
{
    // 析构函数可以用来清理资源
}

void QtRos2Node::send_message()
{
    auto msg = std_msgs::msg::String();
    msg.data = "Hello from Qt and ROS 2!";
    RCLCPP_INFO(node_->get_logger(), "Publishing: '%s'", msg.data.c_str());
    publisher_->publish(msg);
}

int main(int argc, char *argv[])
{
    rclcpp::init(argc, argv);
    auto node = std::make_shared<rclcpp::Node>("qt_ros2_gui_node");
    QApplication app(argc, argv);

    QtRos2Node window(node);
    window.setWindowTitle("ROS 2 Qt GUI");
    window.resize(300, 200);
    window.show();

    return app.exec();
}

#include "qt_ros2_gui_node.moc"  // 确保在源文件的末尾包含这个
